
var User = require('../model/user')
var Shop = require('../model/shop')
var ErrorCode = require('./errorCode')
var SuccessCode = require('./successCode')
var ShopAddr = require('../model/shopAddr')

var shopAddrApi = {
    create: function (req, res) {
        var userid = req.userid
        var addr = req.body.addr
        var detail = req.body.detail
        var phone = req.body.phone
        if (phone) {
            var shopAddr = {
                addr: req.body.addr,
                detail: req.body.detail,
                phone: phone,
                user: userid
            }
            ShopAddr.create(shopAddr, function (err, sa) {
                if (err) {
                    res.json(400, ErrorCode.CommonError)
                    return
                } else {
                    ShopAddr.populate(sa, "user", function (err, shopaddr) {
                        if (err) {
                            res.json(400, ErrorCode.CommonError)
                        } else {
                            res.json(shopaddr)
                            ShopAddr.findOne({ user: userid, isDefalt: true }, function (err, addr) {
                                if (!addr) {
                                    shopaddr.isDefalt = true
                                    shopaddr.save()
                                }
                            })
                        }
                    })
                }

            })
        } else {
            User.findOne({ _id: userid }, function (err, user) {
                if (!user) {
                    res.json(400, ErrorCode.UserNotExist)
                    return
                } else {
                    var shopAddr = {
                        addr: req.body.addr,
                        detail: req.body.detail,
                        phone: user.phone,
                        user: userid
                    }
                    ShopAddr.create(shopAddr, function (err, sa) {
                        if (err) {
                            res.json(400, ErrorCode.CommonError)
                            return
                        } else {
                            ShopAddr.populate(sa, "user", function (err, shopaddr) {
                                if (err) {
                                    res.json(400, ErrorCode.CommonError)
                                } else {
                                    res.json(shopaddr)
                                    ShopAddr.findOne({ user: userid, isDefalt: true }, function (err, addr) {
                                        if (!addr) {
                                            shopaddr.isDefalt = true
                                            shopaddr.save()
                                        }
                                    })
                                }
                            })
                        }

                    })
                }
            })
        }
    },
    delete: function (req, res) {
        var addrid = req.body.id
        ShopAddr.remove({ _id: addrid, user: req.userid }, function (err) {
            if (err) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                res.json(SuccessCode.CommonSuccess)
                return
            }
        })
    }
    ,
    update: function (req, res) {
        var userid = req.userid
        var addrid = req.body.id
        var addr = req.body.addr
        var detail = req.body.detail
        var phone = req.body.phone

        ShopAddr.findOne({ _id: addrid }, function (err, sa) {
            if (err) {
                res.json(400, ErrorCode.CommonError)
                return
            } else {
                sa.phone = phone
                sa.detail = detail
                sa.addr = addr
                sa.save(function (err) {
                    if (err) {
                        res.json(400, ErrorCode.CommonError)
                        return
                    } else {
                        res.json(sa)
                    }
                })
            }
        })
    }
    ,
    list: function (req, res) {
        var userid = req.userid
        ShopAddr.find({ user: userid })
            .populate('user')
            .exec(function (err, list) {
                res.json(list)
                return
            })
    },
    setDefalt: function (req, res) {
        var userid = req.userid
        var addrid = req.body.id
        ShopAddr.findOne({ user: userid, isDefalt: true }, function (err, shopaddr) {

            ShopAddr.findOne({user:userid,_id:addrid}, function (err, sa) {
                if (!sa) {
                    res.json(400,ErrorCode.CommonError)
                    return;
                } else {
                    if (shopaddr) {
                        shopaddr.isDefalt = false
                        shopaddr.save()
                    }
                    sa.isDefalt = true
                    sa.save(function(err){
                        res.json(SuccessCode.CommonSuccess)
                    })
                }
            })
        })
    },
    getDefaultAddr: function (req, res) {
        ShopAddr.findOne({ user: req.userid,isDefalt:true})
            .populate('user')
            .exec(function (err, sa) {
                if (sa) {
                    res.json(sa)
                } else {
                    res.json(400, ErrorCode.DefalutAddrNotExistError)
                }
            })
    }

}
module.exports = shopAddrApi